package cn.com.thingo.hyintelligent.presenter;

import javax.inject.Inject;

import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.LoginContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.LoginBean;
import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import io.reactivex.functions.Consumer;

public class LoginPresenter extends RxPresenter<LoginContract.View> implements LoginContract.Presenter {


    private DataManager dataManager;


    @Inject
    public LoginPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void login(String password, String telephone) {
        addSubscribe(dataManager.login(password, telephone)
                .compose(RxUtil.<LoginBean>rxSchedulerHelper())
                .subscribe(new Consumer<LoginBean>() {
                    @Override
                    public void accept(LoginBean loginBean) throws Exception {
                        if (loginBean.getStatus() == 1) {
                            mView.viewLogin(loginBean);
                        } else {
                            ToastUtil.shortShow(loginBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.d(throwable.getMessage());
                    }
                }));
    }
}
